package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.R;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
@Api(tags = "地址簿相关接口")//Controller类上
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增地址接口")
    public R<String> saveAddress(@RequestBody AddressBook addressBook){
        addressBook.setUserId(ThreadLocalUtil.getCurrentId());
        log.info("addressBook:{}",addressBook.toString());
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     * 显示地址簿
     * @return
     */
    @GetMapping("/list")
    public R addressBookList(){
        List<AddressBook> addressBookList = addressBookService.list();
        return R.success(addressBookList);
    }

    /**
     * 修改默认地址
     * @return
     */
    @PutMapping("/default")
    public R setDefaultAddress(@RequestBody AddressBook addressBook){
       //1.查询当前默认地址id(isDefault=1)
        LambdaUpdateWrapper<AddressBook> lqw=new LambdaUpdateWrapper<>();
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook currentAddress = addressBookService.getOne(lqw);
        if(currentAddress!=null){
            Long currentAddressId=currentAddress.getId();
            changeDefaultCondition(currentAddressId,1);
        }

        //2.改变地址的默认状态，保证只有一个值为1
        Long addressBookId = addressBook.getId();
        log.info("id:{}",addressBook);
        addressBook= addressBookService.getById(addressBookId);
        Integer isDefault = addressBook.getIsDefault();
        changeDefaultCondition(addressBookId, isDefault);
        return R.success("修改成功");
    }

    //改变isDefault状态
    @ApiOperation(value ="地址默认状态更改接口" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "ID",required = true),
            @ApiImplicitParam(name = "isDefault",value = "状态",required = true)
    })
    public void changeDefaultCondition(Long id, Integer isDefault) {
        LambdaUpdateWrapper<AddressBook> luw=new LambdaUpdateWrapper();
        luw.set(AddressBook::getIsDefault,isDefault==1?0:1)
                .eq(AddressBook::getId,id);
        addressBookService.update(luw);
    }

    /**
     * 查询单个地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "地址单个查询接口")
    @ApiImplicitParam(name = "id",value = "ID",required = true)
    public R addressFindOne(@PathVariable Long id){
        log.info("id:{}",id);
        AddressBook  addressBook= addressBookService.getById(id);
        log.info("address:{}",addressBook);
        return R.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public  R updateAddress(@RequestBody AddressBook addressBook){
        log.info("address:{}",addressBook);
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        luw.eq(AddressBook::getId,addressBook.getId());
        addressBookService.update(addressBook,luw);
        return R.success("修改成功");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids){
        //能否删除默认地址
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public R getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> lqw=new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getIsDefault,1);
        AddressBook address= addressBookService.getOne(lqw);
        return R.success(address);
    }

}
